﻿@model IEnumerable<CoreLims.Models.CoreLims.CommonUnit>

@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">单位代码*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Unit-AddUnit-Code" class="form-control font-rfs border-radius-noleft" placeholder="单位代码不能重复" />
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">单位类型*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
                <select id="Unit-AddUnit-Select-Type" class="form-control font-rfs border-radius-noleft" style="width:90%" onchange="selectOnchang(this)">
                    @foreach (var item in Model)
                    {
                        <option value=@item.Code>@item.Type</option>
                    }
                </select>
            <div class="col-11 pl-0 input-group position-absolute float-left" ><input id="Unit-AddUnit-Input-Type" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%"  placeholder="可输入新的类型"/></div>
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">英文名称</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Unit-AddUnit-EnglishName" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">中文名称</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Unit-AddUnit-ChineseName" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Unit-AddUnit-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div><br />
    <div class="text-right">
        <button id="Unit-AddUnit-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Unit-AddUnit-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>
    $("#Unit-AddUnit-Select-Type").prepend(new Option("", ""));
    $("#Unit-AddUnit-Select-Type").val("");

    function selectOnchang(obj) {
        $("#Unit-AddUnit-Input-Type").val($("#Unit-AddUnit-Select-Type").find("option:selected").text());
    };

    $("#Unit-AddUnit-confirm").click(function () {
        if ($("#Unit-AddUnit-Code").val().replace(/[\s]/g, '').length == 0) {
            alert("单位代码不能为空！");
            $("#Unit-AddUnit-Code").addClass('error-style');
            return false;
        }
        if ($("#Unit-AddUnit-Input-Type").val().replace(/[\s]/g, '').length == 0) {
            alert("单位类型不能为空！");
            $("#Unit-AddUnit-Input-Type").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Code"] = $("#Unit-AddUnit-Code").val().replace(/[\s]/g, '');
        jsonStr["Type"] = $("#Unit-AddUnit-Input-Type").val().replace(/[\s]/g, '');
        jsonStr["EnglishName"] = $("#Unit-AddUnit-EnglishName").val();
        jsonStr["ChineseName"] = $("#Unit-AddUnit-ChineseName").val();
        jsonStr["Description"] = $("#Unit-AddUnit-Description").val();

        $.post("/Common/postAddUnit", {
            jsonUnit: JSON.stringify(jsonStr)
        }, function (data) {
            if (data.indexOf("成功") >= 0) {
                $("#Unit-AddUnit-cancel").click();
            } else {
                alert(data);
            }
        });
    });

    $("#Unit-AddUnit-cancel").click(function () {
        var modalID = $("#ModalID").html();
        $("#" + modalID).modal('hide');
    });

</script>
